package com.olap.starter.controller.admin;


import com.olap.starter.core.base.BaseController;
import com.olap.starter.core.response.Result;
import com.olap.starter.pojo.FormMain;
import com.olap.starter.service.FormMainServices;
import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WBG
 * @since 2022-12-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/online/formMain")
public class FormMainController extends BaseController<FormMain, FormMainServices> {

    private final JdbcTemplate jdbcTemplate;

    private final FormMainServices formMainServices;

    @GetMapping("/getTableColumnList")
    public Result getTableColumnList(String tableName) {
        String sql = "SELECT column_name AS `field`,\n" +
                "column_comment,column_comment AS label \n" +
                ",(CASE WHEN column_key = 'PRI' THEN 0 ELSE 1 END) AS visible, column_type FROM information_schema.columns WHERE table_schema = 'online' and table_name in (" + tableName + ") \n" +
                "ORDER BY ordinal_position";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> objectMap : list) {
            String columnType = objectMap.get("column_type").toString();
            if (columnType.indexOf("timestamp") == 0) {//日期+时间
                objectMap.put("domType", "3");
            } else if (columnType.indexOf("int") == 0) {//数字类型
                objectMap.put("domType", "7");
            } else {
                objectMap.put("domType", "1");
            }
        }
        return success(list);
    }

    @PostMapping({"/saveJson"})
    @Transactional
    public Result<?> saveJson(@RequestBody Map<String, Object> params) {
        FormMain formMain = formMainServices.saveJson(params);
        return success(formMain);
    }
}
